package com.team.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.yygh.common.utils.MD5;
import com.team.yygh.hosp.service.HospitalSetService;
import com.team.yygh.model.hosp.HospitalSet;
import com.team.yygh.vo.hosp.HospitalSetQueryVo;
import com.team.yygh.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
@Api("医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet/")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation("获取所有医院的设置信息")
    @GetMapping("findAll")
    public Result findAllHospital() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }



    @ApiOperation("根据ID获取医院设置信息")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }



    @ApiOperation("分页获取所有医院的设置信息")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        // 创建Mybatis-plus提供的Page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        // 构建查询条件对象
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        // 拼接条件
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        // 如果医院名称存在则拼接该条件
        if (StringUtils.hasText(hosname)) {
            wrapper.like("hosname", hosname);
        }
        // 如果医院医院编号存在则拼接该条件
        if (StringUtils.hasText(hoscode)) {
            wrapper.like("hoscode", hoscode);
        }
        // 查询分页数据
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        // 返回查询结果
        return Result.ok(pageHospitalSet);
    }


    @ApiOperation("添加医院设置信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hosptalSet) {
        // 设置状态 1可用 0不可用
        hosptalSet.setStatus(1);
        // 设置签名密钥
        Random random = new Random();
        hosptalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        // 保存信息
        boolean save = hospitalSetService.save(hosptalSet);
        return save ? Result.ok() : Result.fail();
    }



    @ApiOperation("批量逻辑删除医院的设置")
    @DeleteMapping("batchremoveHospSet")
    public Result batchremoveHospSet(@RequestBody List<Long> ids) {
        boolean flag = hospitalSetService.removeByIds(ids);
        return flag ? Result.ok() : Result.fail();
    }


    @ApiOperation("逻辑删除医院的设置")
    @DeleteMapping("removeHospSet/{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }



    @ApiOperation("修改医院的设置信息")
    @PutMapping("updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hosptalSet) {
        boolean flag = hospitalSetService.updateById(hosptalSet);
        return flag ? Result.ok() : Result.fail();
    }


    @ApiOperation("医院设置的锁定和解锁")
    @PutMapping("lookHospitalSet/{id}/{status}")
    public Result look(@PathVariable Long id,
                       @PathVariable Integer status) {
        // 根据id查询出医院
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 设置医院状态
        hospitalSet.setStatus(status);
        // 修改医院信息
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok() : Result.fail();
    }


    @ApiOperation("发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送短信,待完善
        return Result.ok();
    }


}
